package com.littora.alarm.watch;

import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

import java.io.File;
import java.io.FileFilter;

public class FileMonitor {
    private FileAlterationMonitor monitor;

    public FileMonitor(long interval) {
        monitor = new FileAlterationMonitor(interval);
    }
    public void monitor(String path,FileStatusCallback callback) {
        monitor(path,null,callback);
    }
    /**
     * 给文件添加监听
     *
     * @param path     文件路径
     */
    public void monitor(String path, FileFilter fileFilter, FileStatusCallback callback) {
        FileAlterationObserver observer = new FileAlterationObserver(new File(path),fileFilter);
        monitor.addObserver(observer);
        observer.addListener(new FileListener(callback));
    }

    public void stop() throws Exception {
        monitor.stop();
    }

    public void start() throws Exception {
        monitor.start();

    }
}
